<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.tianxiabuyi.*" %>
<%@ page import="com.tianxiabuyi.persist.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.tianxiabuyi.utils.*" %>
<%@ page import="java.io.Serializable" %>
<%!
    // 展示医院详情
    public Message showHospital(Input input) {
        Message msg = new Message();
        Integer hospital = input.getInt("hospital", -1); // 医院编号
        List<Map> hospitalIntroList = QBuilder.db("basedb").select("new map(hi.name as name, hi.phone as phone, hi.address as address, hi.introduction as introduction)").from("hospital_intro hi").where(new Where("hi.hospital=", hospital).and("hi.type=", 2)).page(1, 1).query().getResultObj();
        if (isEmpty(hospitalIntroList)) {
            msg.setErrcodeAndErrmsg(ErrcodeUtil.NO_RESULT, "没有医院信息");
        } else {
            msg.put("data", hospitalIntroList.get(0));
        }
        return msg;
    }

    // 更新医院详情
    public Message updateHospital(Input input) {
        Message msg = new Message();
        Integer hospital = input.getInt("hospital", -1); // 医院编号
        String name = input.getDesc("name", ""); 		 // 医院名称
        String phone = input.getDesc("phone", "");		 // 医院电话
        String address = input.getDesc("address", "");   // 医院地址
        String addressDetail = input.getDesc("address_detail", ""); // 详细地址
        String[] addressArray = address.split("\\|");

        Map<String, String> addressMap = new HashMap<String, String>();
        addressMap.put("province", "");
        addressMap.put("city", "");
        addressMap.put("district", "");
        try {
            addressMap.put("province", Tools.getDesc(addressArray[0], ""));
            addressMap.put("city", Tools.getDesc(addressArray[1], ""));
            addressMap.put("district", Tools.getDesc(addressArray[2], ""));
        } catch (Exception e) {

        }
        addressMap.put("address_detail", addressDetail);

        String introduction = input.getDesc("introduction", ""); // 医院简介
        List<Map> checkList = QBuilder.db("basedb").queryHql("from hospital_intro hi where hi.hospital = " + hospital + " and hi.type = 2");
        if (isEmpty(checkList)) {
            // 新增医院详情
            Map hospitalIntro = new HashMap();
            hospitalIntro.put("name", name);
            hospitalIntro.put("phone", phone);
            hospitalIntro.put("address", JSONUtil.toJson(addressMap));
            hospitalIntro.put("introduction", introduction);
            hospitalIntro.put("hospital", hospital + "");
            hospitalIntro.put("imgs", "[]");
            hospitalIntro.put("create_time", System.currentTimeMillis() + "");
            hospitalIntro.put("type", "2");
            hospitalIntro.put("json", "{}");
            Serializable key = DBTools.db("basedb").addItem("hospital_intro", hospitalIntro);
            if (key == null) {
                msg.setErrcodeAndErrmsg(ErrcodeUtil.FAILURE, "保存医院详情失败");
            }
        } else {
            // 更新医院详情
            Map hospitalIntro = checkList.get(0);
            hospitalIntro.put("name", name);
            hospitalIntro.put("phone", phone);
            hospitalIntro.put("address", JSONUtil.toJson(addressMap));
            hospitalIntro.put("introduction", introduction);
            DBTools.db("basedb").modItem("hospital_intro", hospitalIntro);
        }
        return msg;
    }
%>
